import java.util.Scanner;

public class ReservationSystem {
    
    public static Fleet populateFleet(Fleet fleet) {
        fleet.addPlane(1, 4, 10, "SMF - LAX");
        fleet.addPlane(2, 5, 11, "SMF - LAX1");
        fleet.addPlane(3, 6, 12, "SMF - LAX2");
        fleet.addPlane(4, 7, 13, "SMF - LAX3");
        fleet.addPlane(5, 8, 14, "SMF - LAX4");
        fleet.addPlane(6, 9, 15, "SMF - LAX5");
        fleet.addPlane(7, 10, 16, "SMF - LAX6");
        fleet.addPlane();

        return fleet;
    }
    
	public static void main(String argv[]) {
        //Create and Populate Fleet for Reservations
        Fleet fleet = new Fleet();
        fleet = populateFleet(fleet);
        
        //Create scanner variable to handle input
        Scanner option = new Scanner(System.in);
        
        //Begin control loop for front end interface
        while (true) {
            //Print top level menu
            System.out.println("Reservation System");
            System.out.println();
            System.out.println("Enter '1' to display Fleet List");
            System.out.println("Enter '2' to select Plane (Requires Flight Number)");
            System.out.println("Enter '3' to exit");
            System.out.println();
            System.out.print("Choice: ");
            
            //Capture input
            int selection = option.nextInt();
            System.out.println(); //Add new line after input
            
            //Begin Top level interface
            if (selection == 1) {
                System.out.println("Fleet List - Flight Number and Route");
                System.out.println();
                fleet.printFleet();
                System.out.println();
            } else if (selection == 2) {
                System.out.print("Enter the Flight Number to select a Plane: ");
                
                //Capture input
                selection = option.nextInt();
                System.out.println(); //Add new line after input
                
                boolean checkReservation = true;
                
                while (checkReservation) {
                    //Confirm that Selected FlightNum exists in Fleet
                    if (fleet.findPlane(selection)) {
                        //Plane found, pull Plane
                        Plane plane = fleet.getPlane(selection);
                        
                        //Check to see if Plane is full
                        if (plane.isPlaneFull()) {
                            System.out.println("Plane is full. Next flight leaves in 3 hours.");
                            System.out.println();
                            break;
                        } else {
                            //Begin menu for working on Plane
                            while (checkReservation) {
                                System.out.println("Make Reservations for flight #" 
                                    + plane.getFlightNum() + " for "
                                    + plane.getRoute());
                                System.out.println();
                                
                                if (!plane.isPlaneFull()) {
                                    if (plane.isFirstClassAvailable()) {
                                        System.out.println("Enter '1' for first class.");
                                    }
                                    if (plane.isEconomyAvailable()) {
                                        System.out.println("Enter '2' for economy.");
                                    }
                                }
                                
                                System.out.println("Enter '3' to verify a reservation");
                                System.out.println("Enter '4' to complete reservation.");
                                System.out.println();
                                System.out.print("Choice: ");

                                //Capture input
                                selection = option.nextInt();
                                System.out.println(); //Add new line after input  
                                
                                // reserve first class
                                if (selection == 1) {
                                    // first class is full
                                    if(!plane.addFirstClassReservation()) 
                                    {
                                        if (plane.isEconomyAvailable()) {
                                            System.out.println("First Class is full. Choose economy");
                                            System.out.print("1. Yes, 2. No. Your choice: ");
                                            
                                            selection = option.nextInt();
                                            System.out.println();
                                            
                                            if (selection == 1) {
                                                plane.addEconomyReservation();
                                                System.out.printf("Economy Class. Seat #%d Reservered\n\n",
                                                        plane.getLastSeatReserved());
                                            }
                                            
                                            else if(selection == 2) {
                                                System.out.println("Next flight leaves in 3 hours.");
                                                System.out.println();
                                            }
                                        } else {
                                            System.out.println("Plane is full, Next flight leaves in 3 hours.");
                                            System.out.println();
                                        }
                    
                                    }
                                    else
                                        System.out.printf("First class. Seat #%d Reserved\n\n", 
                                                    plane.getLastSeatReserved());
                                }
                                
                                // reserve economy
                                else if (selection == 2) {
                    
                                    // economy class is full
                                    if(!plane.addEconomyReservation()) {
                                        if (plane.isFirstClassAvailable()) {
                                            System.out.println("Economy is full. Choose First class?");
                                            System.out.print("1. Yes, 2. No. Your choice: " );
                                            
                                            selection = option.nextInt();
                                            System.out.println();
                        
                                            if (selection == 1) {
                                                plane.addFirstClassReservation();
                                                System.out.printf( "First Class. Seat #%d Reserved\n\n",
                                                        plane.getLastSeatReserved());
                                            }
                                            
                                            else if(selection == 2) {
                                                System.out.println( "Next flight leaves in 3 hours.");
                                                System.out.println();
                                            }
                                        } else {
                                            System.out.println("Plane is full, Next flight leaves in 3 hours."); 
                                            System.out.println();
                                        }                      
                                        
                                    }
                                    else
                                        System.out.printf("Economy class. Seat #%d Reserved\n\n",
                                                plane.getLastSeatReserved());
                                }
                    
                                // verify reservation
                                else if(selection == 3) {
                                    
                                    System.out.print("Enter seat number: ");
                                    
                                    selection = option.nextInt();
                                    System.out.println();
                                    
                                    if(plane.checkReservation(selection))
                                        System.out.printf("Seat %1$d is reserved.\n\n", selection);
                                    else
                                        System.out.printf("Seat %1$d is available.\n\n", selection);
                                }            
                                
                                // complete reservation
                                else if (selection == 4) {
                                    System.out.println("Reservation complete.");
                                    fleet.setPlane(plane);
                                    checkReservation = false;
                                }
                            }
                            
                            System.out.println();
                        }
                    } else {
                        System.out.println("Flight #" + selection + " does not exist");
                           checkReservation = false;
                    }
                }
                
                System.out.println();
                
            } else if (selection == 3) {
                break; //exit program
            }
        }
    }

}
